package com.ae.shield.common.items.shieldFittings.rectifier;

import com.ae.shield.AEShieldMod;
import com.ae.shield.api.IShield;
import com.ae.shield.common.items.util.ItemNBTHelper;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DamageSource;
import net.minecraft.util.Hand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class RectifierMakerBase extends Item implements IShield {
    private final String TAG_LEVEL = "level";
    protected final int maxLevel;

    public RectifierMakerBase(int maxLevel) {
        super(new Properties().maxStackSize(1).group(AEShieldMod.AE_SHIELD));
        this.maxLevel = maxLevel;
    }

    @Override
    public void addInformation(@Nonnull ItemStack stackIn, @Nullable World worldIn, @Nonnull List<ITextComponent> components, @Nonnull ITooltipFlag flagIn) {
        components.add(new TranslationTextComponent("tooltip.ae.shield.level")
                .appendString(" " + TextFormatting.BLUE + getLevel(stackIn) + TextFormatting.GRAY + "/" + TextFormatting.WHITE + maxLevel)
                .mergeStyle(TextFormatting.GRAY));
        addRectifierInformation(stackIn, components);
    }

    public void addRectifierInformation(ItemStack stack, List<ITextComponent> components) {

    }

    @Override
    public float onEntityHurt(LivingEntity livingEntity, ItemStack stack, float amount, DamageSource source, IEnergyStorage energyCap) {
        return rectifyDamage(stack, amount, source, energyCap);
    }

    /**
     * 对输入的伤害进行修正，以达到各种目的
     *
     * @return 修正后的伤害
     */
    public float rectifyDamage(ItemStack stack, float amount, DamageSource source, IEnergyStorage energyCap) {
        return amount;
    }

    public boolean onEntityDeath(ItemStack stack, LivingEntity livingEntity, DamageSource source, IEnergyStorage energyStorage) {
        return false;
    }

    public int getLevel(ItemStack stack) {
        return ItemNBTHelper.getInt(stack, TAG_LEVEL, 1);
    }

    public void setLevel(ItemStack stack, int level) {
        ItemNBTHelper.setInt(stack, TAG_LEVEL, Math.max(Math.min(level, maxLevel), 1));
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, @Nonnull Hand handIn) {
        ItemStack heldItem = playerIn.getHeldItem(handIn);
        if (handIn == Hand.MAIN_HAND) {
            if (!worldIn.isRemote) {
                if (playerIn.isCreative()) {
                    setLevel(heldItem, getLevel(heldItem) < maxLevel ? getLevel(heldItem) + 1 : 1);
                    return ActionResult.resultSuccess(heldItem);
                }
            }
        }
        return ActionResult.resultPass(playerIn.getHeldItem(handIn));
    }
}
